package com.action.service.pressenter;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.action.childbase.ChildBasePressent;
import com.action.entity.Redpacket;
import com.action.entity.SystemNotice;
import com.action.service.ChildRestServer;
import com.action.ui.activity.OpenPacketActivity;
import com.base.app_core.base.ActionPacketView;
import com.base.app_core.base.BaseView;
import com.base.app_core.model.Config;
import com.base.app_core.model.Paginated;
import com.base.app_core.model.RedPacketStatusVo;
import com.base.app_core.model.dto.Advertiser;
import com.base.app_core.model.dto.CosVo;
import com.base.app_core.service.Retrofit.RestServer;
import com.base.app_core.service.rx.Callback;
import com.base.app_core.util.AppConstants;
import com.base.app_core.util.XYBAUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

/**
 * Created by jiansheng.li on 2018/3/17.
 */

public class IndexPressenter extends ChildBasePressent<BaseView> {
    @Inject
    public IndexPressenter(ChildRestServer mRestfull, RestServer server) {
        super(mRestfull, server);
    }

    //获取附近的红包
    public void fetchNearRedPacket() {
        invoke(mChildRest.fetchNearRedPacket(), new Callback<List<Redpacket>>() {
            @Override
            public void onResponse(List<Redpacket> data, Paginated paginated,Object extraData) {
                if (isnotNull()) {
                    if (XYBAUtils.isEmpty(data)) {
                        mLifecycle.get().notifyData(new ArrayList<Redpacket>(), Redpacket.class, null);
                    } else {
                        super.onResponse(data, paginated,extraData);
                    }
                }
            }
        });
    }

    public void fetchSystemNotice() {
        invoke(mChildRest.fetchSystemNotice(), new Callback<List<SystemNotice>>() {
            @Override
            public void onResponse(List<SystemNotice> data, Paginated paginated,Object extraData) {
                if (data == null) {
                    mLifecycle.get().notifyData(data, SystemNotice.class, null);
                } else
                    super.onResponse(data, paginated,extraData);
            }
        });
    }


    public void getCosKey() {
        invoke(mRestfull.indexGetCosKey(), new Callback<CosVo>() {
            @Override
            public void onResponse(CosVo data, Paginated paginated,Object extraData) {
                super.onResponse(data, paginated,extraData);
            }
        });
    }

    public void indexGetAdvertiseInfo() {
        Map<String, Object> map = new HashMap<>();
        map.put(AppConstants.FIELD.AD_TYPE, AppConstants.Const.SPREASH_TYPE);
        invoke(mRestfull.indexGetAdvertiseInfo(map), new Callback<List<Advertiser>>() {
            @Override
            public void onResponse(List<Advertiser> data, Paginated paginated,Object extraData) {
                super.onResponse(data, paginated,extraData);
            }
        });
    }

    public void getSystemConfig() {
        invoke(mRestfull.getSystemConfig(), new Callback<List<Config>>() {
            @Override
            public void onResponse(List<Config> data, Paginated paginated,Object extraData) {
                if (isnotNull()) {
                    if (data == null) {
                        mLifecycle.get().notifyData(data, Config.class, null);
                    } else
                        super.onResponse(data, paginated,extraData);
                }
            }
        });
    }

    public void checkStatus() {
        invoke(mRestfull.checkStatus(), new Callback<RedPacketStatusVo>() {
            @Override
            public void onResponse(RedPacketStatusVo data, Paginated paginated,Object extraData) {
                if (data == null) {
                    mLifecycle.get().notifyData(data, RedPacketStatusVo.class, null);
                } else
                    super.onResponse(data, paginated,extraData);
            }
        });
    }

    private int timeOutCount = 0;
    protected Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (null != msg)
                switch (msg.what) {
                    case OpenPacketActivity.COUNT_TIME:
                        timeOutCount -= 1000;
                        if (isnotNull()) {
                            if (timeOutCount >= 0) {
                                ((ActionPacketView.IndexAction) mLifecycle.get()).onTimeCount(timeOutCount);
                                if (handler != null) {
                                    handler.sendEmptyMessageDelayed(OpenPacketActivity.COUNT_TIME, 1000);
                                }
                            } else {
                                ((ActionPacketView.PacketTimeAction) mLifecycle.get()).onTimeCount(-1);
                            }
                        }
                        break;
                    default:
                        break;
                }
        }
    };
}
